#!/usr/bin/env python
# coding=utf-8
"""从 各个给定服务器中拉取 各个用户的crontab信息 tomcat信息 iptables信息, 将结果存放到文件"""
import argparse
import sys
import datetime
from subprocess import Popen, PIPE, STDOUT

RESULT_PATH = "/Users/tzp/Documents/private/workspace/Trial-7-Lan/PythonTest/serverStatus/data/"

SERVER_LIST = ["2297", "2294", "2097", "h31", "d49", "d50"]
SERVER_USER_MAP = {
    "2297":["dmp", "root"],
    "2294":["dmp", "root"],
    "2097":["dmp", "root"],
    "h31":["dmp", "root"],
    "d49":["dmp", "root"],
    "d50":["dmp", "root"],
}

def main():
    arg_parser = argparse.ArgumentParser(
        description="从 各个给定服务器中拉取 各个用户的crontab信息 tomcat信息 iptables信息, 将结果存放到文件")

    arg_parser.add_argument(
        "-as", "--all-server", default=False, action='store_true', help='拉取所有服务器')
    arg_parser.add_argument("-s", "--server", help='指定服务器,无需.mzhen.cn')

    arg_parser.add_argument("-ai", "--all-info", default=False, action='store_true', help='拉取所有信息')
    arg_parser.add_argument("-c", "--crontab", default=False, action='store_true')
    arg_parser.add_argument("-t", "--tomcat", default=False, action='store_true')
    arg_parser.add_argument("-i", "--iptables", default=False, action='store_true')

    args = arg_parser.parse_args()
    print(args)

    if args.all_server:
        servers = SERVER_LIST
    elif args.server:
        servers = [args.server]
    else:
        print("未指定server")
        sys.exit(1)

    if args.all_info:
        crontab(servers)
        tomcat(servers)
        iptables(servers)
    else:
        if args.crontab:
            crontab(servers)
        if args.tomcat:
            tomcat(servers)
        if args.iptables:
            iptables(servers)
    
    print("FechServerStatus END")

def crontab(servers):
    CMD_FORMAT = "ssh %(user)s@%(server)s 'crontab -l'"
    for server in servers:
        cmd = CMD_FORMAT % {"user": "root", "server": server}
        print(cmd)
        result_file = RESULT_PATH + server + "/crontab"

def tomcat(servers):
    pass

def iptables(servers):
    pass

if __name__ == '__main__':
    main()
